// subPackages/packageA/shesu/freeze/freeze.ts
import { ZCC_TYPE } from "../../../../../typings/types/common";
import { getEquityFreezeDropdownApi } from "../../api/shesu";
import { ZCC } from "../../../../utils/BasePage";
import { zcc } from "../../../../utils/util";
import { LitigationListViewModel, LitigationVM } from "../../litigation/litigationVM";

interface PageData {
  companyId: string
  scene: string
  query: string
  navTitle: string
  placeholderText: string
  executionMatters?: string
  dropdownDatas: ZCC_TYPE.Option[]
}
new ZCC.RefreshListPage<any>({

  onChange() {
    this.onPullDownRefresh()
  },

  onClick(e: any) {
    const orign = e.currentTarget.dataset.orign
    const scene = this.data.scene
    const navtitle = this.data.navTitle
    zcc.goLitigationDetail(orign.id, scene, navtitle, JSON.stringify(orign))
  },
  // 搜索框输入关键字
  searchChange(e: any) {
    console.log(this.data.query)
    this.onPullDownRefresh()
  },
  async getDropdownDatas() {
    try {
      const res = await getEquityFreezeDropdownApi(this.data.companyId)
      if (res.content) {
        console.log(res.content)
        let items: ZCC_TYPE.Option[] = [{ label: '全部', value: '' }]
        items = items.concat(res.content.executionMatters.map((item) => {
          return {
            label: item,
            value: item
          }
        }))
        this.setData({
          dropdownDatas: items
        })
      }
    } finally { }
  },
  getListDatas() {
    const vm = new LitigationListViewModel()
    const that = this
    const params = {
      companyId: this.data.companyId,
      query: this.data.query,
      page: this.data.page,
      executionMatters: this.data.executionMatters || ''
    }
    this.setRefreshing()
    vm.loadDataForScene(this.data.scene, params, (res: LitigationVM.Result) => {
      // console.log('res = ' + JSON.stringify(res))
      that.updateDatasWithNewItems(res.items, res.total, res.hasNextPage)
    },
      () => {
        that.endRefreshing()
      })
  },
  /**
   * 页面的初始数据
   */
  data: <PageData>{
    companyId: '',
    query: '',
    navTitle: '',
    placeholderText: '',
    executionMatters: '',
    dropdownDatas: [{ label: '全部', value: '' }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    const vm = new LitigationListViewModel()
    this.setData({
      companyId: this.options.companyId,
      scene: this.options.scene,
      navTitle: vm.getTitle(this.options.scene) || '',
      placeholderText: vm.getSearchPlaceholder(this.options.scene) || ''
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.getDropdownDatas()
    this.getListDatas()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    if (this.resetPageIfNeed()) {
      this.getListDatas()
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.getListDatas()
  },

})